#include <iostream>

#include "rclcpp/rclcpp.hpp"
#include "rclcpp/wait_for_message.hpp"
#include "std_msgs/msg/int32.hpp"

class SubscriptionNode : public rclcpp::Node {
public:
  SubscriptionNode(std::string name) : Node(name) {}
  void start() {
    while (true) {
      rclcpp::sleep_for(std::chrono::seconds(1));
      auto message = std_msgs::msg::Int32();
      // if (!rclcpp::wait_for_message(message, this->shared_from_this(),
      // "topic_queue", std::chrono::seconds(5)))
      if (!rclcpp::wait_for_message(message, this->shared_from_this(),
                                    "topic_queue")) {
        RCLCPP_ERROR(this->get_logger(), "wait for message failed...");
      } else {
        RCLCPP_INFO(this->get_logger(), "get number %d", message.data);
      }
    }
  }
};

int main(int argc, char **argv) {
  rclcpp::init(argc, argv);
  auto node = std::make_shared<SubscriptionNode>("sub_wait_for_message");
  node->start();
  rclcpp::spin(node);
  rclcpp::shutdown();

  return 0;
}